package com.platform.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.platform.entity.ApiUserVideoVo;
import com.platform.entity.UserEntity;
import com.platform.entity.UserVideoEntity;
import com.platform.service.UserService;
import com.platform.service.UserVideoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;

/**
 * 会员视频管理Controller
 *
 * @author zhurusen
 * @email 524227263@qq.com
 * @date 2018-07-02 01:49:42
 */
@Controller
@RequestMapping("uservideo")
public class UserVideoController {
    @Autowired
    private UserVideoService apiUserVideoService;
    @Autowired
    private UserService userService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("uservideo:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<UserVideoEntity> apiUserVideoList = apiUserVideoService.queryList(query);
        for (UserVideoEntity info : apiUserVideoList) {
            UserEntity userEntity = userService.queryObject(info.getUserId());
            if (userEntity != null) {
                info.setUserName(userEntity.getNickname());
            } else {
                info.setUserName("-");
            }
        }


        int total = apiUserVideoService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(apiUserVideoList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("uservideo:info")
    @ResponseBody
    public R info(@PathVariable("id") Long id) {
        UserVideoEntity apiUserVideo = apiUserVideoService.queryObject(id);

        return R.ok().put("apiUserVideo", apiUserVideo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("uservideo:save")
    @ResponseBody
    public R save(@RequestBody UserVideoEntity apiUserVideo) {
        apiUserVideo.setCreateDate(new Date());
        apiUserVideo.setLike(0);
        apiUserVideo.setLocalhostSave(0);
        apiUserVideo.setView(29);
        apiUserVideoService.save(apiUserVideo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("uservideo:update")
    @ResponseBody
    public R update(@RequestBody UserVideoEntity apiUserVideo) {
        apiUserVideoService.update(apiUserVideo);
        return R.ok();
    }

    @RequestMapping("/auding")
    @ResponseBody
    public R auding(@RequestBody UserVideoEntity apiUserVideo) {
        UserVideoEntity info = apiUserVideoService.queryObject(apiUserVideo.getId());
        info.setIsshow(apiUserVideo.getIsshow());
        apiUserVideoService.update(info);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("uservideo:delete")
    @ResponseBody
    public R delete(@RequestBody Long[] ids) {
        apiUserVideoService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<UserVideoEntity> list = apiUserVideoService.queryList(params);

        return R.ok().put("list", list);
    }
}
